home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / batch / inputenv.lha / InputEnv / InputEnv.mod.fmt < prev    next >
Encoding:
Text File  |  1994-05-15  |  8.6 KB  |  171 lines

  1.      (****************************************************************)
  2.     (* InputEnv.mod                            *)
  3.     (* Version 2.0 of InputEnv                    *)
  4.     (* Versions 1.0, 1.1 written in C;                *)
  5.     (* Versions 2.0 and above written in Modula-2            *)
  6.     (* Copyright © 1994 John E> Perry, ///                *)
  7.     (* This program and its source are freely distributable        *)
  8.      (****************************************************************)
  9.     (* This program is a DOS interface, with prompting, for the    *)
  10.     (* user to set an environment variable.  For example, I use it    *)
  11.     (* in my S:Shell-Startup file like so:                *)
  12.     (*                                *)
  13.     (* InputEnv SUPPRESS SHELL UserName Please input your name:    *)
  14.     (* Prompt "$UserName @ *E[1m$Hostname*E[0m : *E[33;3m%S*E[0m> "    *)
  15.     (*                                *)
  16.     (* which gives the prompt (special styles excluded :)        *)
  17.     (*                                *)
  18.     (*    jack@Sara : >                        *)
  19.     (*                                *)
  20.     (* for whatever shell I'm using.  (Sara is the name of my Amiga.*)
  21.      (****************************************************************)
  22.     (* Suggestions, Bugs, Questions, Comments, Snide Remarks, Etc.:    *)
  23.     (*            jep@nauvax.ucc.nau.edu            *)
  24.      (****************************************************************)
  25.  
  26. MODULE InputEnv;
  27.  
  28.      (****************************************************************)
  29.     (* Imported modules.                        *)
  30.      (****************************************************************)
  31.    
  32.   FROM System IMPORT argv, argc;
  33.   FROM SYSTEM IMPORT ADDRESS, ADR;
  34.   FROM Terminal IMPORT WriteString, WriteLn, Write, Read;
  35.   FROM Strings IMPORT ConcatString, CompareString, Relation, StringLength;
  36.   FROM AmigaDOS2 IMPORT SetVar, GVBLocalOnly, GVBGlobalOnly,
  37.         DOSVariableFlagsSet, GetVar;
  38.  
  39.      (****************************************************************)
  40.     (* Programmer-defined constants.                *)
  41.     (* These are special characters that I need to define like so    *)
  42.     (* in order to achieve various desired effects with a Write.    *)
  43.     (* Or... defined for my convenience.                *)
  44.      (****************************************************************)
  45.   CONST
  46.     EOS = CHAR(0);    (* end of string    *)
  47.     TAB = CHAR(9);    (* tab            *)
  48.     EOL = CHAR(10);    (* end of line        *)
  49.     ESC = CHAR(27);    (* escape        *)
  50.  
  51.      (****************************************************************)
  52.     (* Module Variables.                        *)
  53.     (* yesNo: an input variable (yes or no)                *)
  54.     (* counter: a string counter                    *)
  55.     (* envVar: name of the environment variable being created    *)
  56.     (* envString: string AmigaDOS is to associate with envVar    *)
  57.     (* suppressFlag: suppress "...overwrite?" prompt?        *)
  58.     (* shellFlag: shell or globale environment variable?        *)
  59.     (* promptString: how to prompt user                *)
  60.     (* argString: used to point to command-line arguments        *)
  61.      (****************************************************************)
  62.   VAR
  63.     yesNo: CHAR;
  64.     counter: CARDINAL;
  65.     envVar: ARRAY[0..63] OF CHAR;
  66.     envString: ARRAY[0..255] OF CHAR;
  67.     suppressFlag, shellFlag: BOOLEAN;
  68.     promptString: ARRAY[0..127] OF CHAR;
  69.     argString: POINTER TO ARRAY [0..63] OF CHAR;
  70.  
  71.      (****************************************************************)
  72.     (* PROCEDURE ReadLn(VAR string: ARRAY OF CHAR);            *)
  73.     (* Reads string until EOL is encountered.            *)
  74.      (****************************************************************)
  75.   PROCEDURE ReadLn(VAR string: ARRAY OF CHAR);
  76.     VAR
  77.       in: CHAR;        (* input character    *)
  78.       loc: CARDINAL;    (* location in string    *)
  79.     BEGIN
  80.       loc := 0;
  81.       in := CHAR(0);
  82.       (* read string *)
  83.       WHILE in # CHAR(10)
  84.         DO
  85.       Read(in);
  86.       string[loc] := in;
  87.       INC(loc);
  88.     END(* WHILE *)
  89.       string[loc - 1] := CHAR(0);
  90.     END ReadLn;
  91.  
  92.   BEGIN
  93.     IF argc = 1
  94.       THEN
  95.         WriteString('Dude! The usage is:'); WriteLn;
  96.         Write(TAB); WriteString('InputEnv [SUPPRESS] [SHELL] <envname> <prompt>'); 
  97.     WriteLn;
  98.         WriteString('where <envname> is the name of the environment string and'); 
  99.     WriteLn;
  100.         WriteString('<prompt> is the prompt for the user.  ');
  101.     Write(ESC); 
  102.     WriteString('[3mNOTE THAT <prompt> MUST'); WriteLn;
  103.         WriteString('BE THE LAST THING ON THE LINE, AND <envname> ');
  104.     Write(ESC); 
  105.     WriteString('[1mMUST '); 
  106.     Write(ESC);
  107.     WriteString('[22m BE NEXT TO LAST!!!');
  108.     Write(ESC);
  109.     WriteString('[23m'); 
  110.     WriteLn;
  111.         Write(TAB); 
  112.     WriteString('To suppress verification of new environment variable,'); 
  113.     WriteLn;
  114.         WriteString('use SUPPRESS, and to make a shell instead of a global'); 
  115.     WriteLn;
  116.         WriteString('environment variable, user SHELL.'); 
  117.     WriteLn;
  118.     WriteLn;
  119.         WriteString('InputEnv v2.0 Copyright © 1994 John E> Perry, ///'); WriteLn;
  120.     HALT;
  121.       END(* IF *)
  122.     (* process command line *)
  123.     counter := 1;
  124.     argString := ADDRESS(argv^[1]);
  125.     (* suppress "...overwrite?" prompt? *)
  126.     IF CompareString(argString^, 'SUPPRESS') = equal
  127.       THEN
  128.         suppressFlag := TRUE;
  129.     INC(counter);
  130.       END(* IF *)
  131.     argString := ADDRESS(argv^[counter]);
  132.     (* shell variable? default is global *)
  133.     IF CompareString(argString^, 'SHELL') = equal
  134.       THEN
  135.     shellFlag := TRUE;
  136.     INC(counter);
  137.       END(* IF *)
  138.     ConcatString(envVar, argv^[counter]^);
  139.     INC(counter);
  140.     (* prompt *)
  141.     WHILE counter <= (argc - 1)
  142.       DO
  143.         ConcatString(promptString, argv^[counter]^);
  144.         ConcatString(promptString, ' ');
  145.     INC(counter);
  146.       END(* WHILE *)
  147.     (* get string *)
  148.     WriteString(promptString);
  149.     ReadLn(envString);
  150.     (* check to see if variable exists *)
  151.     IF NOT suppressFlag
  152.       THEN
  153.         IF NOT ((GetVar(ADR(envVar), ADR(promptString), 63, DOSVariableFlagsSet {GVBGlobalOnly}) = LONGINT-(1DH)) OR (GetVar(ADR(envVar), ADR(promptString), 63, DOSVariableFlagsSet {GVBLocalOnly}) = LONGINT-(1DH)))
  154.       THEN
  155.         WriteString('Variable already exists! overwrite? (Y/N) ');
  156.         Read(yesNo);
  157.         IF (yesNo = 'N') OR (yesNo = 'n')
  158.           THEN
  159.             HALT;
  160.           END(* IF *)
  161.       END(* IF *)
  162.       END(* IF *)
  163.     (* okay, write it out *)
  164.     IF shellFlag
  165.       THEN
  166.     shellFlag := SetVar(ADR(envVar), ADR(envString), LONGINT(StringLength(envString)), DOSVariableFlagsSet{GVBLocalOnly});
  167.       ELSE
  168.     shellFlag := SetVar(ADR(envVar), ADR(envString), LONGINT(StringLength(envString)), DOSVariableFlagsSet{GVBGlobalOnly});
  169.       END(* IF *)
  170.   END InputEnv.
  171.